package com.sajus.web.billing.ejb.mapper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.sajus.billing.client.dto.AccountAttributeDTO;
import com.sajus.billing.client.dto.AccountDTO;
import com.sajus.billing.client.dto.BillDTO;
import com.sajus.billing.client.dto.BillItemDTO;
import com.sajus.billing.client.dto.DiscountDTO;
import com.sajus.billing.client.dto.OfferingDTO;
import com.sajus.billing.client.dto.PriceInfoDTO;
import com.sajus.billing.client.dto.PromoOfferingDTO;
import com.sajus.billing.client.dto.PromotionDTO;
import com.sajus.billing.client.dto.RateMappingDTO;
import com.sajus.billing.client.dto.ServiceAttributeDTO;
import com.sajus.billing.client.dto.ServiceDTO;
import com.sajus.billing.client.dto.ServiceTypeDTO;
import com.sajus.billing.client.dto.UsageItemLineDTO;
import com.sajus.web.billing.ejb.data.model.Account;
import com.sajus.web.billing.ejb.data.model.AccountAttribute;
import com.sajus.web.billing.ejb.data.model.Bill;
import com.sajus.web.billing.ejb.data.model.BillItem;
import com.sajus.web.billing.ejb.data.model.Discount;
import com.sajus.web.billing.ejb.data.model.Offering;
import com.sajus.web.billing.ejb.data.model.PriceInfo;
import com.sajus.web.billing.ejb.data.model.PromoOffering;
import com.sajus.web.billing.ejb.data.model.Promotion;
import com.sajus.web.billing.ejb.data.model.RateMapping;
import com.sajus.web.billing.ejb.data.model.Service;
import com.sajus.web.billing.ejb.data.model.ServiceAttribute;
import com.sajus.web.billing.ejb.data.model.ServiceType;
import com.sajus.web.billing.ejb.data.model.UsageItemLine;

public class ModelToDTOMapper {


	public static DiscountDTO map( Discount dis ){
		DiscountDTO ret = new DiscountDTO();
		if ( dis == null )
			return null;

		ret.setAmount( dis.getAmount() );
		ret.setDescription( dis.getDescription() );
		ret.setId( dis.getId() );
		ret.setRate( dis.getRate() );
		ret.setStartQuantity( dis.getStartQuantity() );
		ret.setType( dis.getType() );

		return ret;

	}
	
	public static RateMappingDTO map( RateMapping rate ){
		RateMappingDTO ret = new RateMappingDTO();
		
		if ( rate == null )
			return null;
		
		ret.setBaseRate( rate.getBaseRate() );
		ret.setId( rate.getId() );
		ret.setMaxcharge( rate.getMaxcharge() );
		ret.setMaxunits( rate.getMaxunits() );
		ret.setMincharge( rate.getMincharge() );
		ret.setMinunits( rate.getMinunits() );
		ret.setRateKey( rate.getRateKey() );
		ret.setScheme_id( rate.getScheme_id() );
		ret.setServiceName( rate.getServiceName() );
		ret.setStep1rate( rate.getStep1rate() );
		ret.setStep1units( rate.getStep1units() );
		ret.setStep2rate( rate.getStep2rate() );
		ret.setStep2units( rate.getStep2units() );
		
		return ret;
		
	}

	public static OfferingDTO map( Offering off ){
		OfferingDTO ret = new OfferingDTO();

		if ( off == null )
			return null;

		ret.setDescription( off.getDescription() );
		ret.setId( off.getId() );
		ret.setServiceType( map( off.getServiceType() ) );
		ret.setBaseRate( off.getBaseRate() );
		ret.setLimit( off.getLimits() );
		ret.setMaxRate( off.getMaxRate() );
		ret.setMaxUnit( off.getMaxUnit() );
		ret.setMinRate( off.getMinRate() );
		ret.setMinUnit( off.getMinUnit() );
		ret.setPostRule( off.getPostRule() );
		ret.setPreRule( off.getPreRule() );
		ret.setStep1Rate( off.getStep1Rate() );
		ret.setStep1Unit( off.getStep1Unit() );
		ret.setStep2Rate( off.getStep2Rate() );
		ret.setStep2Unit( off.getStep2Unit() );
		

		return ret;
	}

	public static PriceInfoDTO map( PriceInfo prcIn ){
		PriceInfoDTO ret = new PriceInfoDTO();

		if ( prcIn == null )
			return null;

		ret.setCurrency( prcIn.getCurrency() );
		ret.setDescription( prcIn.getDescription() );
		ret.setId( prcIn.getId() );
		ret.setPrice( prcIn.getPrice() );
		ret.setStep_price( prcIn.getStep_price() );
		ret.setStep_price_1( prcIn.getStep_price_1() );
		ret.setStep_price_2( prcIn.getStep_price_2() );
		ret.setStep_price_3( prcIn.getStep_price_3() );
		ret.setStep_price_4( prcIn.getStep_price_4() );
		ret.setStep_qty( prcIn.getStep_qty() );
		ret.setStep_qty_1( prcIn.getStep_qty_1() );
		ret.setStep_qty_2( prcIn.getStep_qty_2() );
		ret.setStep_qty_3( prcIn.getStep_qty_3() );
		ret.setStep_qty_4( prcIn.getStep_qty_4() );

		return ret;
	}


	public static PromoOfferingDTO map( PromoOffering pro ){
		PromoOfferingDTO ret = new PromoOfferingDTO();

		if ( pro == null )
			return null;

		ret.setDescription( pro.getDescription() );
		ret.setFinalOffering( map( pro.getFinalOffering() ) );
		ret.setId( pro.getId() );
		ret.setInitialOffering( map( pro.getInitialOffering() ) );
		
		ret.setServicetype( map( pro.getServicetype() ) );

		return ret;
	}

	public static PromotionDTO map( Promotion pro ){
		PromotionDTO ret = new PromotionDTO();

		if ( pro == null )
			return null;

		ret.setDescription( pro.getDescription() );
		ret.setDuration( pro.getDuration() );
		ret.setEndDate( pro.getEndDate() );
		ret.setId( pro.getId() );
		ret.setName( pro.getName() );
		HashSet<PromoOfferingDTO> offerings = new HashSet<PromoOfferingDTO>();
		
		for ( PromoOffering off : pro.getOfferings() ){
			offerings.add( map(off) );
		}

		ret.setOfferings( offerings );
		ret.setStartDate( pro.getStartDate() );

		return ret;

	}

	public static ServiceTypeDTO map( ServiceType ser ){
		ServiceTypeDTO ret = new ServiceTypeDTO();

		if ( ser == null )
			return null;

		ret.setDescription( ser.getDescription() );
		ret.setId( ser.getId() );
		ret.setServicecolumn( ser.getServicecolumn() );
		ret.setServicetable( ser.getServicetable() );
		ret.setType( ser.getType() );
		ret.setUnit( ser.getUnit() );
		ret.setIdentifier( ser.getIdentifier() );
		ret.setChannelId( ser.getChannelId() );

		return ret;

	}

	public static AccountDTO map( Account acc ){
		AccountDTO ret = new AccountDTO();

		if ( acc == null )
			return null;

		ret.setAddressSameOnCreditCard( acc.isAddressSameOnCreditCard() );
		ret.setBillingDay( acc.getBillingDay() );
		ret.setBillingPeriodStartDay( acc.getBillingPeriodStartDay() );
		ret.setBillingPeriodStartTime( acc.getBillingPeriodStartTime() );
		ret.setBillingPeriodType( acc.getBillingPeriodType() );
		ret.setBillingTime( acc.getBillingTime() );
		ret.setCancellationDate( acc.getCancellationDate() );

		ret.setCity( acc.getCity() );
		ret.setCompany( acc.getCompany() );
		ret.setCountry( acc.getCountry() );
		ret.setCounty( acc.getCounty() );
		ret.setCurrency( acc.getCurrency() );
		ret.setEmail( acc.getEmail() );
		ret.setEmailInvoice( acc.isEmailInvoice() );
		ret.setFirstName( acc.getFirstName() );
		ret.setId( acc.getId() );
		ret.setLastName( acc.getLastName() );
		ret.setMailInvoice( acc.isMailInvoice() );
		ret.setNextBillingDate( acc.getNextBillingDate() );
		ret.setPassword( acc.getPassword() );
		ret.setPasswordRecoveryQ1( acc.getPasswordRecoveryQ1() );
		ret.setPasswordRecoveryQ1Answer( acc.getPasswordRecoveryQ1Answer() );
		ret.setPasswordRecoveryQ2( acc.getPasswordRecoveryQ2() );
		ret.setPasswordRecoveryQ2Answer( acc.getPasswordRecoveryQ2Answer() );
		ret.setPayByCreditCard( acc.isPayByCreditCard() );
		ret.setPhone( acc.getPhone() );
		ret.setPostalCode( acc.getPostalCode() );
		ret.setPromoCode( acc.getPromoCode() );


		ret.setProvince( acc.getProvince() );
		ret.setStatus( acc.getStatus() );
		ret.setStreetAddress( acc.getStreetAddress() );
		ret.setStreetAddress2( acc.getStreetAddress2() );
		ret.setSubscriptionDate( acc.getSubscriptionDate() );
		ret.setTransactionReference( acc.getTransactionReference() );
		ret.setType( acc.getType() );


		ArrayList<AccountDTO> childrenAccounts = new ArrayList<AccountDTO>();
		
		if ( acc.getChildrenAccount() != null ) {
			for ( Account child : acc.getChildrenAccount() ){
				System.out.println("Account" + child.getId() );
				AccountDTO childNew =  map( child );
				childNew.setParent( ret );
				childrenAccounts.add( childNew );
			}

			ret.setChildrenAccounts(childrenAccounts);
		}

		HashSet<ServiceDTO> services = new HashSet<ServiceDTO>();
		
		if ( acc.getServices() != null ) {
			for ( Service ser : acc.getServices() ){
				services.add( map( ser ) );
			}
	
			ret.setServices( services );
		}

		HashSet<AccountAttributeDTO> accountAttributes = new HashSet<AccountAttributeDTO>();
		if ( acc.getAccountAttrubites() != null ){
			for ( AccountAttribute child : acc.getAccountAttrubites() ){
				accountAttributes.add( map( child ) );
			}
	
			ret.setAccountAttributes( accountAttributes );
		}
		
		ret.setParent( map_no_child( acc.getParent() ) );

		return ret;
	}

	private static AccountDTO map_no_child(Account acc ) {
		AccountDTO ret = new AccountDTO();

		if ( acc == null )
			return null;

		ret.setAddressSameOnCreditCard( acc.isAddressSameOnCreditCard() );
		ret.setBillingDay( acc.getBillingDay() );
		ret.setBillingPeriodStartDay( acc.getBillingPeriodStartDay() );
		ret.setBillingPeriodStartTime( acc.getBillingPeriodStartTime() );
		ret.setBillingPeriodType( acc.getBillingPeriodType() );
		ret.setBillingTime( acc.getBillingTime() );
		ret.setCancellationDate( acc.getCancellationDate() );

		ret.setCity( acc.getCity() );
		ret.setCompany( acc.getCompany() );
		ret.setCountry( acc.getCountry() );
		ret.setCounty( acc.getCounty() );
		ret.setCurrency( acc.getCurrency() );
		ret.setEmail( acc.getEmail() );
		ret.setEmailInvoice( acc.isEmailInvoice() );
		ret.setFirstName( acc.getFirstName() );
		ret.setId( acc.getId() );
		ret.setLastName( acc.getLastName() );
		ret.setMailInvoice( acc.isMailInvoice() );
		ret.setNextBillingDate( acc.getNextBillingDate() );
		ret.setPassword( acc.getPassword() );
		ret.setPasswordRecoveryQ1( acc.getPasswordRecoveryQ1() );
		ret.setPasswordRecoveryQ1Answer( acc.getPasswordRecoveryQ1Answer() );
		ret.setPasswordRecoveryQ2( acc.getPasswordRecoveryQ2() );
		ret.setPasswordRecoveryQ2Answer( acc.getPasswordRecoveryQ2Answer() );
		ret.setPayByCreditCard( acc.isPayByCreditCard() );
		ret.setPhone( acc.getPhone() );
		ret.setPostalCode( acc.getPostalCode() );
		ret.setPromoCode( acc.getPromoCode() );


		ret.setProvince( acc.getProvince() );
		ret.setStatus( acc.getStatus() );
		ret.setStreetAddress( acc.getStreetAddress() );
		ret.setStreetAddress2( acc.getStreetAddress2() );
		ret.setSubscriptionDate( acc.getSubscriptionDate() );
		ret.setTransactionReference( acc.getTransactionReference() );
		ret.setType( acc.getType() );

		return ret;
	}

	public static AccountAttributeDTO map( AccountAttribute acc ){
		AccountAttributeDTO ret = new AccountAttributeDTO();

		if ( acc == null ){
			return null;
		}

		ret.setId( acc.getId() );
		ret.setName( acc.getName() );
		ret.setValue( acc.getValue() );

		return ret;
	}

	public static ServiceAttributeDTO map( ServiceAttribute ser ){
		ServiceAttributeDTO ret = new ServiceAttributeDTO();

		if ( ser == null ){
			return null;
		}

		ret.setId( ser.getId() );
		ret.setName( ser.getName() );
		ret.setValue( ser.getValue() );
		ret.setService( map_no_child( ser.getService() ) );

		return ret;
	}

	public static ServiceDTO map( Service ser ){
		ServiceDTO ret = new ServiceDTO();

		if ( ser == null ){
			return null;
		}

		ret.setAccount( mapAccount(ser.getAccount() ) );
		ret.setActivationDate( ser.getActivationDate() );
		ret.setCancellationDate( ser.getCancellationDate() );
		ret.setId( ser.getId() );
		ret.setOffering( map( ser.getOffering() ) );
		ret.setStatus( ser.getStatus() );
		ret.setSubscriptionDate( ser.getSubscriptionDate() );
		
		ret.setBaseRate( ser.getBaseRate() );
		ret.setLimit( ser.getLimitsUsage() );
		ret.setMaxRate( ser.getMaxRate() );
		ret.setMaxUnit( ser.getMaxUnit() );
		ret.setMinRate( ser.getMinRate() );
		ret.setMinUnit( ser.getMinUnit() );
		ret.setPostRule( ser.getPostRule() );
		ret.setPreRule( ser.getPreRule() );
		ret.setStep1Rate( ser.getStep1Rate() );
		ret.setStep1Unit( ser.getStep1Unit() );
		ret.setStep2Rate( ser.getStep2Rate() );
		ret.setStep2Unit( ser.getStep2Unit() );

		ArrayList<ServiceAttributeDTO> serviceAttributes = new ArrayList<ServiceAttributeDTO>();
		if ( ser.getServiceAttributes() != null ){
			for ( ServiceAttribute sa : ser.getServiceAttributes() ){
				serviceAttributes.add( map(sa) );
			}
			ret.setServiceAttributes( serviceAttributes );
		}

		return ret;
	}
	
	private static ServiceDTO map_no_child( Service ser ){
		ServiceDTO ret = new ServiceDTO();

		if ( ser == null ){
			return null;
		}

		ret.setAccount( mapAccount(ser.getAccount() ) );
		ret.setActivationDate( ser.getActivationDate() );
		ret.setCancellationDate( ser.getCancellationDate() );
		ret.setId( ser.getId() );
		ret.setOffering( map( ser.getOffering() ) );
		ret.setStatus( ser.getStatus() );
		ret.setSubscriptionDate( ser.getSubscriptionDate() );
		
		ret.setBaseRate( ser.getBaseRate() );
		ret.setLimit( ser.getLimitsUsage() );
		ret.setMaxRate( ser.getMaxRate() );
		ret.setMaxUnit( ser.getMaxUnit() );
		ret.setMinRate( ser.getMinRate() );
		ret.setMinUnit( ser.getMinUnit() );
		ret.setPostRule( ser.getPostRule() );
		ret.setPreRule( ser.getPreRule() );
		ret.setStep1Rate( ser.getStep1Rate() );
		ret.setStep1Unit( ser.getStep1Unit() );
		ret.setStep2Rate( ser.getStep2Rate() );
		ret.setStep2Unit( ser.getStep2Unit() );

		return ret;
	}

	private static AccountDTO mapAccount( Account acc ) {
		AccountDTO ret = new AccountDTO();

		if ( acc == null )
			return null;

		ret.setAddressSameOnCreditCard( acc.isAddressSameOnCreditCard() );
		ret.setBillingDay( acc.getBillingDay() );
		ret.setBillingPeriodStartDay( acc.getBillingPeriodStartDay() );
		ret.setBillingPeriodStartTime( acc.getBillingPeriodStartTime() );
		ret.setBillingPeriodType( acc.getBillingPeriodType() );
		ret.setBillingTime( acc.getBillingTime() );
		ret.setCancellationDate( acc.getCancellationDate() );

		ret.setCity( acc.getCity() );
		ret.setCompany( acc.getCompany() );
		ret.setCountry( acc.getCountry() );
		ret.setCounty( acc.getCounty() );
		ret.setCurrency( acc.getCurrency() );
		ret.setEmail( acc.getEmail() );
		ret.setEmailInvoice( acc.isEmailInvoice() );
		ret.setFirstName( acc.getFirstName() );
		ret.setId( acc.getId() );
		ret.setLastName( acc.getLastName() );
		ret.setMailInvoice( acc.isMailInvoice() );
		ret.setNextBillingDate( acc.getNextBillingDate() );
		ret.setPassword( acc.getPassword() );
		ret.setPasswordRecoveryQ1( acc.getPasswordRecoveryQ1() );
		ret.setPasswordRecoveryQ1Answer( acc.getPasswordRecoveryQ1Answer() );
		ret.setPasswordRecoveryQ2( acc.getPasswordRecoveryQ2() );
		ret.setPasswordRecoveryQ2Answer( acc.getPasswordRecoveryQ2Answer() );
		ret.setPayByCreditCard( acc.isPayByCreditCard() );
		ret.setPhone( acc.getPhone() );
		ret.setPostalCode( acc.getPostalCode() );
		ret.setPromoCode( acc.getPromoCode() );

		ret.setProvince( acc.getProvince() );
		ret.setStatus( acc.getStatus() );
		ret.setStreetAddress( acc.getStreetAddress() );
		ret.setStreetAddress2( acc.getStreetAddress2() );
		ret.setSubscriptionDate( acc.getSubscriptionDate() );
		ret.setTransactionReference( acc.getTransactionReference() );
		ret.setType( acc.getType() );


		ArrayList<AccountDTO> childrenAccounts = new ArrayList<AccountDTO>();
		if ( acc.getChildrenAccount() != null ) {
			for ( Account child : acc.getChildrenAccount() ){
				System.out.println("Account" + acc.getId() );
	
				childrenAccounts.add( map( child ) );
			}
		}

		ret.setChildrenAccounts(childrenAccounts);

		HashSet<AccountAttributeDTO> accountAttributes = new HashSet<AccountAttributeDTO>();
		if ( acc.getAccountAttrubites() != null ) {
			for ( AccountAttribute child : acc.getAccountAttrubites() ){
				accountAttributes.add( map( child ) );
			}
		}

		ret.setAccountAttributes( accountAttributes );

		return ret;
	}

	public static BillDTO map( Bill bill ){
		BillDTO ret = new BillDTO();

		if ( bill == null ){
			return null;
		}

		ret.setAccount( map( bill.getAccount() ) );
		ret.setAmountDue( bill.getAmountDue() );
		ret.setBillingPeriodEnd( bill.getBillingPeriodEnd() );
		ret.setBillingPeriodStart( bill.getBillingPeriodStart() );
		ret.setChargeAdjustments( bill.getChargeAdjustments() );
		ret.setCurrency( bill.getCurrency() );
		ret.setDate( bill.getDate() );
		ret.setId( bill.getId() );
		ret.setPreviousPayments( bill.getPreviousPayments() );
		ret.setTotalAmount( bill.getTotalAmount() );

		ArrayList<BillItemDTO> items = new ArrayList<BillItemDTO>();
		for ( BillItem item : bill.getItems() ){
			items.add( map( item ) );
		}

		ret.setItems( items );
		return ret;

	}
	
	private static BillDTO map_no_child( Bill bill ){
		BillDTO ret = new BillDTO();

		if ( bill == null ){
			return null;
		}

		ret.setAccount( map( bill.getAccount() ) );
		ret.setAmountDue( bill.getAmountDue() );
		ret.setBillingPeriodEnd( bill.getBillingPeriodEnd() );
		ret.setBillingPeriodStart( bill.getBillingPeriodStart() );
		ret.setChargeAdjustments( bill.getChargeAdjustments() );
		ret.setCurrency( bill.getCurrency() );
		ret.setDate( bill.getDate() );
		ret.setId( bill.getId() );
		ret.setPreviousPayments( bill.getPreviousPayments() );
		ret.setTotalAmount( bill.getTotalAmount() );

		return ret;

	}
	
	private static Bill map_no_child( BillDTO bill ){
		Bill ret = new Bill();

		if ( bill == null ){
			return null;
		}

		ret.setAccount( map( bill.getAccount() ) );
		ret.setAmountDue( bill.getAmountDue() );
		ret.setBillingPeriodEnd( bill.getBillingPeriodEnd() );
		ret.setBillingPeriodStart( bill.getBillingPeriodStart() );
		ret.setChargeAdjustments( bill.getChargeAdjustments() );
		ret.setCurrency( bill.getCurrency() );
		ret.setDate( bill.getDate() );
		ret.setId( bill.getId() );
		ret.setPreviousPayments( bill.getPreviousPayments() );
		ret.setTotalAmount( bill.getTotalAmount() );

		return ret;

	}

	private static BillItem map_no_child( BillItemDTO billItem ) {
		BillItem ret = new BillItem();

		if ( billItem == null ){
			return null;
		}

		ret.setAmount( billItem.getAmount() );
		ret.setDescription( billItem.getDescription() );
		ret.setDiscountRate( billItem.getDiscountRate() );
		ret.setDiscountType( billItem.getDiscountType() );
		ret.setId( billItem.getId() );
		ret.setJurisdiction( billItem.getJurisdiction() );
		ret.setLineNumber( billItem.getLineNumber() );
		ret.setProrated( billItem.isProrated() );
		ret.setProratedUsage( billItem.getProratedUsage() );
		ret.setServiceReference( billItem.getServiceReference() );
		ret.setTaxRate( billItem.getTaxRate() );
		ret.setTaxType( billItem.getTaxType() );
		ret.setType( billItem.getType() );
		
		return ret;
	}
	
	private static BillItemDTO map_no_child( BillItem billItem ) {
		BillItemDTO ret = new BillItemDTO();

		if ( billItem == null ){
			return null;
		}

		ret.setAmount( billItem.getAmount() );
		ret.setDescription( billItem.getDescription() );
		ret.setDiscountRate( billItem.getDiscountRate() );
		ret.setDiscountType( billItem.getDiscountType() );
		ret.setId( billItem.getId() );
		ret.setJurisdiction( billItem.getJurisdiction() );
		ret.setLineNumber( billItem.getLineNumber() );
		ret.setProrated( billItem.isProrated() );
		ret.setProratedUsage( billItem.getProratedUsage() );
		ret.setServiceReference( billItem.getServiceReference() );
		ret.setTaxRate( billItem.getTaxRate() );
		ret.setTaxType( billItem.getTaxType() );
		ret.setType( billItem.getType() );
		
		return ret;
	}
	
	public static BillItemDTO map( BillItem billItem ){
		BillItemDTO ret = new BillItemDTO();

		if ( billItem == null ){
			return null;
		}

		ret.setAmount( billItem.getAmount() );
		ret.setDescription( billItem.getDescription() );
		ret.setDiscountRate( billItem.getDiscountRate() );
		ret.setDiscountType( billItem.getDiscountType() );
		ret.setId( billItem.getId() );
		ret.setJurisdiction( billItem.getJurisdiction() );
		ret.setLineNumber( billItem.getLineNumber() );
		ret.setProrated( billItem.isProrated() );
		ret.setProratedUsage( billItem.getProratedUsage() );
		ret.setServiceReference( billItem.getServiceReference() );
		ret.setTaxRate( billItem.getTaxRate() );
		ret.setTaxType( billItem.getTaxType() );
		ret.setType( billItem.getType() );
		
		List<UsageItemLineDTO> list = new ArrayList<UsageItemLineDTO>();
		if ( billItem.getUsageLineItems() != null ) {
			for ( UsageItemLine usage : billItem.getUsageLineItems() ) {
				list.add( map( usage ) );
			}
			
			ret.setUsageLineItems( list );
		}
		
		ret.setBill( map_no_child( billItem.getBill() ) );
		
		return ret;
	}


	public static Discount map( DiscountDTO dis ){
		Discount ret = new Discount();
		if ( dis == null )
			return null;

		ret.setAmount( dis.getAmount() );
		ret.setDescription( dis.getDescription() );
		ret.setId( dis.getId() );
		ret.setRate( dis.getRate() );
		ret.setStartQuantity( dis.getStartQuantity() );
		ret.setType( dis.getType() );

		return ret;

	}

	public static Offering map( OfferingDTO off ){
		Offering ret = new Offering();

		if ( off == null )
			return null;

		ret.setDescription( off.getDescription() );
		ret.setId( off.getId() );
		ret.setServiceType( map( off.getServiceType() ) );
		ret.setBaseRate( off.getBaseRate() );
		ret.setLimits( off.getLimit() );
		ret.setMaxRate( off.getMaxRate() );
		ret.setMaxUnit( off.getMaxUnit() );
		ret.setMinRate( off.getMinRate() );
		ret.setMinUnit( off.getMinUnit() );
		ret.setPostRule( off.getPostRule() );
		ret.setPreRule( off.getPreRule() );
		ret.setStep1Rate( off.getStep1Rate() );
		ret.setStep1Unit( off.getStep1Unit() );
		ret.setStep2Rate( off.getStep2Rate() );
		ret.setStep2Unit( off.getStep2Unit() );
		

		return ret;
	}

	public static PriceInfo map( PriceInfoDTO prcIn ){
		PriceInfo ret = new PriceInfo();

		if ( prcIn == null )
			return null;

		ret.setCurrency( prcIn.getCurrency() );
		ret.setDescription( prcIn.getDescription() );
		ret.setId( prcIn.getId() );
		ret.setPrice( prcIn.getPrice() );
		ret.setStep_price( prcIn.getStep_price() );
		ret.setStep_price_1( prcIn.getStep_price_1() );
		ret.setStep_price_2( prcIn.getStep_price_2() );
		ret.setStep_price_3( prcIn.getStep_price_3() );
		ret.setStep_price_4( prcIn.getStep_price_4() );
		ret.setStep_qty( prcIn.getStep_qty() );
		ret.setStep_qty_1( prcIn.getStep_qty_1() );
		ret.setStep_qty_2( prcIn.getStep_qty_2() );
		ret.setStep_qty_3( prcIn.getStep_qty_3() );
		ret.setStep_qty_4( prcIn.getStep_qty_4() );

		return ret;
	}


	public static PromoOffering map( PromoOfferingDTO pro ){
		PromoOffering ret = new PromoOffering();

		if ( pro == null )
			return null;

		ret.setDescription( pro.getDescription() );
		ret.setFinalOffering( map( pro.getFinalOffering() ) );
		ret.setId( pro.getId() );
		ret.setInitialOffering( map( pro.getInitialOffering() ) );
		ret.setPromotion( map( pro.getPromotion() ) );
		ret.setServicetype( map( pro.getServicetype() ) );

		return ret;
	}

	public static Promotion map( PromotionDTO pro ){
		Promotion ret = new Promotion();

		if ( pro == null )
			return null;

		ret.setDescription( pro.getDescription() );
		ret.setDuration( pro.getDuration() );
		ret.setEndDate( pro.getEndDate() );
		ret.setId( pro.getId() );
		ret.setName( pro.getName() );

		ret.setStartDate( pro.getStartDate() );

		return ret;

	}

	public static ServiceType map( ServiceTypeDTO ser ){
		ServiceType ret = new ServiceType();

		if ( ser == null )
			return null;

		ret.setDescription( ser.getDescription() );
		ret.setId( ser.getId() );
		ret.setServicecolumn( ser.getServicecolumn() );
		ret.setServicetable( ser.getServicetable() );
		ret.setType( ser.getType() );
		ret.setUnit( ser.getUnit() );
		ret.setIdentifier( ser.getIdentifier() );
		ret.setChannelId( ser.getChannelId() );

		return ret;

	}

	public static Account map( AccountDTO acc ){
		Account ret = new Account();

		if ( acc == null )
			return null;

		ret.setAddressSameOnCreditCard( acc.isAddressSameOnCreditCard() );
		ret.setBillingDay( acc.getBillingDay() );
		ret.setBillingPeriodStartDay( acc.getBillingPeriodStartDay() );
		ret.setBillingPeriodStartTime( acc.getBillingPeriodStartTime() );
		ret.setBillingPeriodType( acc.getBillingPeriodType() );
		ret.setBillingTime( acc.getBillingTime() );
		ret.setCancellationDate( acc.getCancellationDate() );

		ret.setCity( acc.getCity() );
		ret.setCompany( acc.getCompany() );
		ret.setCountry( acc.getCountry() );
		ret.setCounty( acc.getCounty() );
		ret.setCurrency( acc.getCurrency() );
		ret.setEmail( acc.getEmail() );
		ret.setEmailInvoice( acc.isEmailInvoice() );
		ret.setFirstName( acc.getFirstName() );
		ret.setId( acc.getId() );
		ret.setLastName( acc.getLastName() );
		ret.setMailInvoice( acc.isMailInvoice() );
		ret.setNextBillingDate( acc.getNextBillingDate() );
		ret.setPassword( acc.getPassword() );
		ret.setPasswordRecoveryQ1( acc.getPasswordRecoveryQ1() );
		ret.setPasswordRecoveryQ1Answer( acc.getPasswordRecoveryQ1Answer() );
		ret.setPasswordRecoveryQ2( acc.getPasswordRecoveryQ2() );
		ret.setPasswordRecoveryQ2Answer( acc.getPasswordRecoveryQ2Answer() );
		ret.setPayByCreditCard( acc.isPayByCreditCard() );
		ret.setPhone( acc.getPhone() );
		ret.setPostalCode( acc.getPostalCode() );
		ret.setPromoCode( acc.getPromoCode() );
		ret.setProvince( acc.getProvince() );
		ret.setStatus( acc.getStatus() );
		ret.setStreetAddress( acc.getStreetAddress() );
		ret.setStreetAddress2( acc.getStreetAddress2() );
		ret.setSubscriptionDate( acc.getSubscriptionDate() );
		ret.setTransactionReference( acc.getTransactionReference() );
		ret.setType( acc.getType() );

		HashSet<Service> services = new HashSet<Service>();
		if ( acc.getServices() != null ) {
			for ( ServiceDTO ser : acc.getServices() ){
				Service save = map( ser );
				save.setAccount( ret );
				services.add( save );
			}

			ret.setServices( services );
		}

		HashSet<AccountAttribute> atts = new HashSet<AccountAttribute>();
		if ( acc.getAccountAttributes() != null ){
			for ( AccountAttributeDTO att : acc.getAccountAttributes() ){
				AccountAttribute save = map( att );
				save.setAccount( ret );
				atts.add( save );
			}
			
			ret.setAccountAttrubites( atts );
		}
		
		List<Account> children = new ArrayList<Account>();
		if ( acc.getChildrenAccounts() != null ){
			
		}
		
		return ret;
	}

	public static AccountAttribute map( AccountAttributeDTO acc ){
		AccountAttribute ret = new AccountAttribute();

		if ( acc == null ){
			return null;
		}

		ret.setId( acc.getId() );
		ret.setName( acc.getName() );
		ret.setValue( acc.getValue() );

		return ret;
	}

	public static ServiceAttribute map( ServiceAttributeDTO ser ){
		ServiceAttribute ret = new ServiceAttribute();

		if ( ser == null ){
			return null;
		}

		ret.setId( ser.getId() );
		ret.setName( ser.getName() );
		ret.setValue( ser.getValue() );
		ret.setService( map_no_child( ser.getService() ) );

		return ret;
	}

	public static Service map( ServiceDTO ser ){
		Service ret = new Service();

		if ( ser == null ){
			return null;
		}

		ret.setAccount( map(ser.getAccount() ) );
		ret.setActivationDate( ser.getActivationDate() );
		ret.setCancellationDate( ser.getCancellationDate() );
		ret.setId( ser.getId() );
		ret.setOffering( map( ser.getOffering() ) );
		ret.setStatus( ser.getStatus() );
		ret.setSubscriptionDate( ser.getSubscriptionDate() );
		ret.setBaseRate( ser.getBaseRate() );
		ret.setLimitsUsage( ser.getLimit() );
		ret.setMaxRate( ser.getMaxRate() );
		ret.setMaxUnit( ser.getMaxUnit() );
		ret.setMinRate( ser.getMinRate() );
		ret.setMinUnit( ser.getMinUnit() );
		ret.setPostRule( ser.getPostRule() );
		ret.setPreRule( ser.getPreRule() );
		ret.setStep1Rate( ser.getStep1Rate() );
		ret.setStep1Unit( ser.getStep1Unit() );
		ret.setStep2Rate( ser.getStep2Rate() );
		ret.setStep2Unit( ser.getStep2Unit() );

		ArrayList<ServiceAttribute> serviceAttributes = new ArrayList<ServiceAttribute>();
		if ( ser.getServiceAttributes() != null ){
			
			for ( ServiceAttributeDTO sa : ser.getServiceAttributes() ){
				ServiceAttribute save =  map(sa);
				save.setService( ret );
				serviceAttributes.add( save );
			}
			
			ret.setServiceAttributes(serviceAttributes);
		}

		return ret;
	}
	
	private static Service map_no_child( ServiceDTO ser ){
		Service ret = new Service();

		if ( ser == null ){
			return null;
		}

		ret.setAccount( map(ser.getAccount() ) );
		ret.setActivationDate( ser.getActivationDate() );
		ret.setCancellationDate( ser.getCancellationDate() );
		ret.setId( ser.getId() );
		ret.setOffering( map( ser.getOffering() ) );
		ret.setStatus( ser.getStatus() );
		ret.setSubscriptionDate( ser.getSubscriptionDate() );
		ret.setBaseRate( ser.getBaseRate() );
		ret.setLimitsUsage( ser.getLimit() );
		ret.setMaxRate( ser.getMaxRate() );
		ret.setMaxUnit( ser.getMaxUnit() );
		ret.setMinRate( ser.getMinRate() );
		ret.setMinUnit( ser.getMinUnit() );
		ret.setPostRule( ser.getPostRule() );
		ret.setPreRule( ser.getPreRule() );
		ret.setStep1Rate( ser.getStep1Rate() );
		ret.setStep1Unit( ser.getStep1Unit() );
		ret.setStep2Rate( ser.getStep2Rate() );
		ret.setStep2Unit( ser.getStep2Unit() );

		return ret;
	}

	public static Bill map( BillDTO bill ){
		Bill ret = new Bill();

		if ( bill == null ){
			return null;
		}

		ret.setAccount( map( bill.getAccount() ) );
		ret.setAmountDue( bill.getAmountDue() );
		ret.setBillingPeriodEnd( bill.getBillingPeriodEnd() );
		ret.setBillingPeriodStart( bill.getBillingPeriodStart() );
		ret.setChargeAdjustments( bill.getChargeAdjustments() );
		ret.setCurrency( bill.getCurrency() );
		ret.setDate( bill.getDate() );
		ret.setId( bill.getId() );
		ret.setPreviousPayments( bill.getPreviousPayments() );
		ret.setTotalAmount( bill.getTotalAmount() );

		ArrayList<BillItem> items = new ArrayList<BillItem>();
		for ( BillItemDTO item : bill.getItems() ){
			items.add( map( item ) );
		}

		ret.setItems( items );
		return ret;

	}

	public static BillItem map( BillItemDTO billItem ){
		BillItem ret = new BillItem();

		if ( billItem == null ){
			return null;
		}

		ret.setAmount( billItem.getAmount() );
		ret.setDescription( billItem.getDescription() );
		ret.setDiscountRate( billItem.getDiscountRate() );
		ret.setDiscountType( billItem.getDiscountType() );
		ret.setId( billItem.getId() );
		ret.setJurisdiction( billItem.getJurisdiction() );
		ret.setLineNumber( billItem.getLineNumber() );
		ret.setProrated( billItem.isProrated() );
		ret.setProratedUsage( billItem.getProratedUsage() );
		ret.setServiceReference( billItem.getServiceReference() );
		ret.setTaxRate( billItem.getTaxRate() );
		ret.setTaxType( billItem.getTaxType() );
		ret.setType( billItem.getType() );
		
		List<UsageItemLine> list = new ArrayList<UsageItemLine>();
		
		if ( billItem.getUsageLineItems() != null ) {
			for ( UsageItemLineDTO usage : billItem.getUsageLineItems() ) {
				list.add( map( usage ) );
			}
			ret.setUsageLineItems( list );
		}
		
		ret.setBill( map_no_child( billItem.getBill() ) );
		
		return ret;
	}
	
	public static RateMapping map( RateMappingDTO rate ){
		RateMapping ret = new RateMapping();
		
		if ( rate == null )
			return null;
		
		ret.setBaseRate( rate.getBaseRate() );
		ret.setId( rate.getId() );
		ret.setMaxcharge( rate.getMaxcharge() );
		ret.setMaxunits( rate.getMaxunits() );
		ret.setMincharge( rate.getMincharge() );
		ret.setMinunits( rate.getMinunits() );
		ret.setRateKey( rate.getRateKey() );
		ret.setScheme_id( rate.getScheme_id() );
		ret.setServiceName( rate.getServiceName() );
		ret.setStep1rate( rate.getStep1rate() );
		ret.setStep1units( rate.getStep1units() );
		ret.setStep2rate( rate.getStep2rate() );
		ret.setStep2units( rate.getStep2units() );
		
		return ret;
		
	}
	
	public static UsageItemLine map( UsageItemLineDTO usage ){
		UsageItemLine ret = new UsageItemLine();
		
		if ( usage == null ){
			return null;
		}
		
		ret.setCharge( usage.getCharge() );
		ret.setCharge_dt( usage.getCharge_dt() );
		ret.setId( usage.getId() );
		ret.setUsages( usage.getUsage() );
		ret.setVar0value( usage.getVar0value() );
		ret.setVar1value( usage.getVar1value() );
		ret.setVar2value( usage.getVar2value() );
		ret.setVar3value( usage.getVar3value() );
		ret.setVar4value( usage.getVar4value() );
		ret.setVar5value( usage.getVar5value() );
		ret.setVar6value( usage.getVar6value() );
		ret.setVar7value( usage.getVar7value() );
		ret.setVar8value( usage.getVar8value() );
		ret.setVar9value( usage.getVar9value() );
		
		ret.setBillItem( map_no_child( usage.getBillItem() ) );
		
		return ret;
	}
	
	public static UsageItemLineDTO map( UsageItemLine usage ){
		UsageItemLineDTO ret = new UsageItemLineDTO();
		
		if ( usage == null ){
			return null;
		}
		
		ret.setCharge( usage.getCharge() );
		ret.setCharge_dt( usage.getCharge_dt() );
		ret.setId( usage.getId() );
		ret.setUsage( usage.getUsages() );
		ret.setVar0value( usage.getVar0value() );
		ret.setVar1value( usage.getVar1value() );
		ret.setVar2value( usage.getVar2value() );
		ret.setVar3value( usage.getVar3value() );
		ret.setVar4value( usage.getVar4value() );
		ret.setVar5value( usage.getVar5value() );
		ret.setVar6value( usage.getVar6value() );
		ret.setVar7value( usage.getVar7value() );
		ret.setVar8value( usage.getVar8value() );
		ret.setVar9value( usage.getVar9value() );
		
		ret.setBillItem( map_no_child( usage.getBillItem() ) );
		
		return ret;
	}

}
